package cn.bdqn.controller;

import cn.bdqn.domain.Course;
import cn.bdqn.service.CourseService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

/**
 * 课程控制层
 * @author Wang-Hao
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 查询全部课程信息
     * @param pageNum
     * @param pageAmount
     * @return
     */
    @RequestMapping("/queryAllInfoPaging")
    public Result queryCourseAllInfoPaging(Integer pageNum, Integer pageAmount) throws Exception{
        try {
            //调用service层的方法并返回成功的信息以及数据
            Page page = courseService.queryCourseAllInfoPaging(pageNum,pageAmount);
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            //返回错误信息
            throw new Exception(e.getMessage());
        }

    }

    /**
     * 根据课程id查询课程
     * @param courseId
     * @return
     */
    @RequestMapping("/queryCourseByCourseId")
    public Result queryCourseByCourseId(Integer courseId) throws Exception {
        try {
            //调用service层的方法并返回成功的信息以及数据
            return ResultGenerator.genSuccessResult(courseService.queryCourseByCourseId(courseId));
        }catch (Exception e){
            e.printStackTrace();
            //返回错误信息
            throw new Exception(e.getMessage());
        }

    }

    /**
     * 添加课程信息
     * @param course
     * @return
     */
    @RequestMapping("/insertCourseInfo")
    public Result insertCourseInfo(Course course) throws Exception{
        try {
            //调用service层的添加方法添加课程信息
            courseService.insertCourseInfo(course);
            //封装并返回成功的信息
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            //返回错误信息
            throw new Exception(e.getMessage());
        }

    }

    /**
     * 多条件查询课程信息【分页显示】
     * @param pageNum
     * @param pageAmount
     * @param baseAddress
     * @param fitAge
     * @param courseFeature
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/queryInfoByConditions")
    public Result queryInfoByConditions(Integer pageNum, Integer pageAmount,
                                        String baseAddress, String fitAge,
                                        Integer courseFeature) throws Exception {
        try{
            //封装分页信息
            Page page = courseService.queryCourseInfoPagingByConditions(pageNum,pageAmount,baseAddress,fitAge,courseFeature);
            //返回分页信息
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            //返回错误信息
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 根据课程id修改课程状态
     * @param id
     * @return
     */
    @RequestMapping("/updateIssueState")
    public Result updateIssueState(Integer id, Integer issueState)throws Exception {
        try {
            //调用service层的根据课程id更新状态的方法
            courseService.updateIssueState(id,issueState);
            //返回成功信息
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            //返回错误信息
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 更新课程信息
     * @param course
     * @return
     */
    @RequestMapping("/updateCourseData")
    public Result updateCourseData(Course course) throws Exception{
        try{
            //调用service层的更新数据方法
            courseService.updateCourseData(course);
            //返回成功信息
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            //返回错误信息
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 根据基地id查询课程信息
     * @param baseId
     * @return
     */
    @RequestMapping("/queryInfoByBaseId")
    public Result queryInfoByBaseId(Integer baseId, Integer startNumber, Integer pageSize) throws Exception {
        try{
            //调用service层的根据基地id查询课程信息的方法并返回成功信息以及数据
            Page page = courseService.queryCourseInfoByBaseId(baseId,startNumber,pageSize);
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
        }catch (Exception e){
            e.printStackTrace();
            //返回错误信息
            throw new Exception(e.getMessage());
        }

    }

}
